package com.suda.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suda.dao.RouteMapper;
import com.suda.pojo.Route;
import com.suda.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:Route业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class RouteServiceImpl implements RouteService {

    @Autowired
    private RouteMapper routeMapper;


    /**
     * Route条件+分页查询
     * @param route 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Route> findPage(Route route, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(route);
        //执行搜索
        return new PageInfo<Route>(routeMapper.selectByExample(example));
    }

    /**
     * Route分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Route> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Route>(routeMapper.selectAll());
    }

    /**
     * Route条件查询
     * @param route
     * @return
     */
    @Override
    public List<Route> findList(Route route){
        //构建查询条件
        Example example = createExample(route);
        //根据构建的条件查询数据
        return routeMapper.selectByExample(example);
    }


    /**
     * Route构建查询对象
     * @param route
     * @return
     */
    public Example createExample(Route route){
        Example example=new Example(Route.class);
        Example.Criteria criteria = example.createCriteria();
        if(route!=null){
            // 路由id
            if(!StringUtils.isEmpty(route.getRouteId())){
                    criteria.andEqualTo("routeId",route.getRouteId());
            }
            // 货车id
            if(!StringUtils.isEmpty(route.getVehicleId())){
                    criteria.andEqualTo("vehicleId",route.getVehicleId());
            }
            // 到达仓库时间
            if(!StringUtils.isEmpty(route.getArrivalTime())){
                    criteria.andEqualTo("arrivalTime",route.getArrivalTime());
            }
            // 离开仓库时间
            if(!StringUtils.isEmpty(route.getDepartureTime())){
                    criteria.andEqualTo("departureTime",route.getDepartureTime());
            }
            // 仓库id
            if(!StringUtils.isEmpty(route.getWarehouseId())){
                    criteria.andEqualTo("warehouseId",route.getWarehouseId());
            }
            // 前往下一个仓库id
            if(!StringUtils.isEmpty(route.getToWarehouseId())){
                    criteria.andEqualTo("toWarehouseId",route.getToWarehouseId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        routeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Route
     * @param route
     */
    @Override
    public void update(Route route){
        routeMapper.updateByPrimaryKey(route);
    }

    /**
     * 增加Route
     * @param route
     */
    @Override
    public void add(Route route){
        routeMapper.insert(route);
    }

    /**
     * 根据ID查询Route
     * @param id
     * @return
     */
    @Override
    public Route findById(String id){
        return  routeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Route全部数据
     * @return
     */
    @Override
    public List<Route> findAll() {
        return routeMapper.selectAll();
    }
}
